$(document).ready(function() {

	var xinghao = ''
	var lingqu = function(thisq, url_e = null) {
		var lucky_record_id = thisq.attr('data-lucky_record_id');
		if (url_e == null) {
			var productid = thisq.attr('data-product');
		}
		else{
			var productid = url_e.product_id;
		}
		var address_id = getCookie('select_address');
		$('.detail_select').show();
		$('.detail_mask').show();
		$.ajax({
			url: lucky_draw_record_attr,
			type: 'POST',
			data: {
				product_id: productid,
				address_id: address_id
			},
			success: function(data) {
				var data = JSON.parse(data)
				console.log(data)
				$('.detail_select_xh ul').html('')
				for (var i = 0; i < data.data.attr.length; i++) {
					$('.detail_select_xh ul').append('<li data-c="' + data.data.attr[i].attr_id + '">' + data.data.attr[i].attr_name + '</li>')
				}
				var html = ''
				if(data.data.address != ""){
					html += '<div class="name_tel">' +
					'<span class="name">' + data.data.address.name + '</span>' +
					'<span class="tel">' + data.data.address.tel + '</span>' +
					'</div>' +
					'<div class="ads" data-lucky_record_id="' + lucky_record_id + '" data-productid = "' + data.data.attr[0].product_id + '">' +
					'<span class="address" data-address="' + data.data.address.address_id + '">' + data.data.address.province + data.data.address.city + data.data.address.address + '</span>' +
					'</div><img src="'+public_url+'/images/info.png" class="right_info" alt="">'
				}else{
					html += '<div class="name_tel">请填写收货地址</div>'
				}
				
				
				$('.order_address').html(html)
				
				if (url_e !== null && url_e.attr_id != '') { 
					console.log(url_e.attr_id);
					$('.detail_select_xh li[data-c=' + url_e.attr_id + ']').addClass('cur');
					xinghao = url_e.attr_id;
				}else{
					 
				}
			},
			error:function(xhr, textStatus){
				if(xhr.status == 401){
					 window.location.href = "/Npoint_store/Login";
				}else{
					alert('系统错误')
				}

			}
		})
	}
	// $('.lijilingqu').click(function() {
	// 	lingqu($(this))
	// });
	$(document).on('click', '.lijilingqu', function() {
		lingqu($(this))
		$('.detail_select_logo img').attr("src",$(this).attr('data-src'))
	})

	$(document).on('click', '.detail_select_xh li', function() {
		$(this).addClass('cur').siblings().removeClass('cur');
		$('.detail_select_sx').html('已选择:<span class="detail_select_xinghao">'+$(this).html()+'</span>')
		xinghao = $(this).attr('data-c')
	})
	


	// 更换地址
	// 
	$(document).on('click', '.order_content', function() {
		var productid = $(this).find('.ads').attr('data-productid')
		var lucky_record_id = $(this).find('.ads').attr('data-lucky_record_id')
		var arr = {};
		arr.attr_id = xinghao;
		arr.product_id = productid;
		arr.action = 'lucky_draw_record_list';
		arr.lucky_record_id = lucky_record_id;
		var url_extend = JSON.stringify(arr);
		SetCookie('url_extend', url_extend)
		
		window.location.href = addressurl + '?action=lucky_draw_record_list';
	})


	// $('.order_content').click(function(event) {
	// 	var productid = $(this).find('.ads').attr('data-productid')
	// 	var lucky_record_id = $(this).find('.ads').attr('data-lucky_record_id')
	// 	var arr = {};
	// 	arr.attr_id = xinghao;
	// 	arr.product_id = productid;
	// 	arr.action = 'lucky_draw_record_list';
	// 	arr.lucky_record_id = lucky_record_id;
	// 	var url_extend = JSON.stringify(arr);
	// 	SetCookie('url_extend', url_extend)
		
	// 	window.location.href = addressurl + '?action=lucky_draw_record_list';
	// });


// 默认显示状态
	var url_e = getCookie('url_extend')
	if (url_e) {
		var url_e = JSON.parse(getCookie('url_extend'));
		if (url_e.action == 'lucky_draw_record_list') {
			lingqu($('#lijilingqu_' + url_e.lucky_record_id), url_e);
			SetCookie('url_extend', '');
		}
	}

	$('.close ,.jiang_again').click(function(event) {
		$('.mask ,.close ,.jiangimg').css('display', 'none')
	});
	// 关闭
	$('.detail_select_close').click(function(event) {
		$('.detail_select').hide();
		$('.detail_mask').hide();
	});

	$('.lingjiang').click(function(event) {
		var address_id = $(this).parent().find('.address').attr('data-address')
		var product_id = $(this).parent().find('.ads').attr('data-productid')
		var lucky_record_id = $(this).parent().find('.ads').attr('data-lucky_record_id')
		var attr_id = getCookie('url_extend')
		//console.log(attr_id)

		$.ajax({
			url: lingquurl,
			type: 'POST',
			data: {
				address_id: address_id,
				product_id: product_id,
				attr_id: xinghao,
				sorce: 2,
				lucky_record_id: lucky_record_id,
			},
			success:function(data){
				  window.location.reload();

			},
			error:function(xhr, textStatus){
				if(xhr.status == 401){
					 window.location.href = "/Npoint_store/Login";
				}else{
					alert('系统错误')
				}

			}
		})
		
		
	});

	
	var stop = true;
	var page = 1;
function show(){
	$.ajax({
		url: lucky_draw_record,
		type: 'get',
		data: {
			page: page
		},
		success: function(data) {
			var html = '';
			var data = JSON.parse(data)
			console.log(data)
			if (data.code == 1) {
				for (var i = 0; i < data.data.record_lst.length; i++) {
					html += '<li>'+
								'<div class="lucky_draw_recordimg"><img src="'+data.data.record_lst[i].product_thumb_img+'" alt=""></div>'+
								'<div class="lucky_draw_recordtit">'+
									'<h1>'+data.data.record_lst[i].product_title+'</h1>'+
									'<p>'+new Date(data.data.record_lst[i].create_time * 1000).toLocaleString().replace(/\-(\d)\b/g, "-0$1")+'</p>'+
								'</div>';
								if (data.data.record_lst[i].product_type == 2 || data.data.record_lst[i].order_id != 0) {
									html+='<div class="zhuangtai yiling">已领取</div>'
								}else{
									html+=' <div id="lijilingqu_'+data.data.record_lst[i].lucky_record_id+'"  class="zhuangtai lijilingqu" data-product="'+data.data.record_lst[i].product_id+'"  data-lucky_record_id="'+data.data.record_lst[i].lucky_record_id+'" >立即<br> 领取</div>'
								}
								
    				html+='</li>'
				};
			}else{
				$(".Loading").html("数据加载失败");
			}
		
			
			$('.lucky_draw_record ul').append(html)
			stop = true;
			page++;
			if (data.data.record_lst.length < 10) {
				$(".Loading").html("全部加载完！");
				stop = false;
			} else {
				$(".Loading").html("Loading...");
			}
		},
		error:function(xhr, textStatus){
			if(xhr.status == 401){
				 window.location.href = "/Npoint_store/Login";
			}else{
				alert('系统错误')
			}

		}
	})
}
show()
	// loading  加载

	// var status = decodeURIComponent((new RegExp('[?|&]status=' + '([^&;]+?)(&|#|;|$)', "ig").exec(location.search) || [, ""])[1].replace(/\+/g, '%20')) || null;

	$(window).scroll(function() {
		var totalheight = parseFloat($(window).height()) + parseFloat($(window).scrollTop());

		if ($(document).height() <= totalheight) {
			if (stop == true) {
				stop = false;
				show()
			}
		}
	});

	if ($('.lucky_draw_record li').length == 0) {
		$(".Loading").html("暂无记录");
	}else if($('.lucky_draw_record li').length < 10){
		$(".Loading").html("全部加载完！");
	}



});


